﻿using Abp;
using Abp.Domain.Repositories;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.Shop;
using FwProject.Statistics.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using X.PagedList;

namespace FwProject.Statistics
{
    public class StatisticsAppService : FwProjectAppServiceBase, IStatisticsAppService
    {
        private readonly IRepository<ShopOrder, Guid> _shopOrderRepository;
        private readonly IRepository<ShopOrderDetails, Guid> _shopOrderDetailsRepository;
        private readonly IRepository<ShopProduct, Guid> _repositoryShopProduct;
        public StatisticsAppService(IRepository<ShopOrder, Guid> shopOrderRepository, IRepository<ShopOrderDetails, Guid> shopOrderDetailsRepository, IRepository<ShopProduct, Guid> repositoryShopProduct)
        {
            _shopOrderRepository = shopOrderRepository;
            _shopOrderDetailsRepository = shopOrderDetailsRepository;
            _repositoryShopProduct = repositoryShopProduct;
        }

        public async Task<IPagedList<SalesRevenueOutputDto>> GetSalesRevenueDataAsync(int page, int limit, DateTime time)
        {
            var query = _shopOrderRepository.GetAll().Where(o => EF.Functions.DateDiffMonth(time, o.CreationTime) == 0).GroupJoin(_shopOrderDetailsRepository.GetAll(), i => i.Id, j => j.OrderId, (i, j) => new SalesRevenueOutputDto
            {
                OrderNumber = i.OrderNumber,
                PayTotal = i.PayTotal,
                CreationTime = i.CreationTime,
                SalesRevenueDetils = j.Select(o => new SalesRevenueDetilsDto
                {
                    PayPrice = o.PayPrice,
                    ProductCount = o.ProductCount,
                    ShopProductName = o.ShopName
                }).ToList()
            });
            var pagelist = await query.OrderByDescending(o => o.CreationTime).ToPagedListAsync(page, limit);
            return pagelist;

        }


        //public async Task<LayerDataBaseDto> GetHotSaleItemData(int page, int limit, HotSaleItemFilter filter)
        //{
        //    var query=_shopOrderDetailsRepository.GetAll()
        //}

    }
}
